package demo1;

import demo2.Cell;

public  class JudgeMethod {


    //判断四方格能否下落
    public static boolean canDrop(){
        Cell[] cells = Statement.currentOne.cells;
        for (Cell cell : cells) {
            int row = cell.getRow();
            int col = cell.getCol();
            //判断是否到达底部
            if(row == Statement.wall.length - 1){
                return false;
            }else if(Statement.wall[row + 1][col] != null){
                //判断是否有方块
                return false;
            }
        }
        return true;
    }

    //创建消行方法
    public static void destroyLine(){
         SoundPlayer soundPlayer = new SoundPlayer();
        //声明变量，统计当前消除的行数
        int line = 0;
        Cell[] cells = Statement.currentOne.cells;
        for (Cell cell : cells) {
            int row = cell.getRow();
            //判断当前行是否已满
            if(isFullLine(row)){
                line++;
                soundPlayer.playSound(Statement.SP_ELIMINATE);
                for(int i = row;i > 0;i--){
                    System.arraycopy(Statement.wall[i - 1],0, Statement.wall[i],0, Statement.wall[0].length);
                }
                Statement.wall[0] = new Cell[9];
            }
        }

        //在分数池中获取分数，累加到总分数中
        Statement.totalScore += Statement.scores_pool[line];
        //统计消除总行数
        Statement.totalLine += line;
    }

    //判断当前行是否已满
    public static boolean isFullLine(int row){
        Cell[] cells = Statement.wall[row];
        for (Cell cell : cells) {
            if(cell == null){
                return false;
            }
        }
        return true;
    }

    //判断游戏是否结束
    public static boolean isGameOver(){
        Cell[] cells = Statement.nextOne.cells;
        for (Cell cell : cells) {
            int row = cell.getRow();
            int col = cell.getCol();
            if(Statement.wall[row][col] != null){
                SoundPlayer soundPlayer = new SoundPlayer();
                soundPlayer.playSound(Statement.SP_GAMEOVER);
                return true;
            }
        }
        return false;
    }

    //判断游戏是否出界
    public static boolean outOfBounds(){
        Cell[] cells = Statement.currentOne.cells;
        for (Cell cell : cells) {
            int col = cell.getCol();
            int row = cell.getRow();
            if(row < 0 || row > Statement.wall.length - 1 || col < 0 || col > Statement.wall[0].length - 1){
                return true;
            }
        }
        return false;
    }
    //判断方块是否重合
    public static boolean coincide(){
        Cell[] cells = Statement.currentOne.cells;
        for (Cell cell : cells) {
            int row = cell.getRow();
            int col = cell.getCol();
            if(Statement.wall[row][col] != null){
                return true;
            }
        }
        return false;
    }
    public static void judge() {
        if(canDrop()){
            Statement.currentOne.softDrop();
        }else{
            //嵌入到墙中
            CellMethod.landToWall();
            //判断能否消行
            destroyLine();
            //判断游戏是否结束
            if(isGameOver()){
                Statement.game_state = Statement.GAMEOVER;
            }else{
                Statement.currentOne = Statement.nextOne;
                Statement.nextOne = Tetromino.randomOne();
            }
        }
    }

}
